/********************************************************************
 * Copyright(c) 2023-2023 Grayson Zheng.
 * File: bit.h
 * Description: The angle control header file of the SG90 Micro Servo.
 * Version: 1.0
 * Date: 2023-08-11
 * Author: zhengxinyu13@qq.com
 * ---------- Revision History ----------
 * <version>
 * <date>
 * <author>
 * <desc>
 * Modified to be suitable to the new coding rules in all functions.
 ********************************************************************/
 
#include "regx52.h"
#include "STC89C5xRC-rdp.h"
#include "datatype.h"
#include "bit.h"

#ifndef __SG90_MICRO_SERVO_H__
#define __SG90_MICRO_SERVO_H__

#define MACHINE_CYCLE (fp_64)(12000000.00/11059200.00)

/* The default cycle is 20,000us */
#define PWM_CYCLE (uint_16)(20000.00 / MACHINE_CYCLE)

/* The default duty cycle is 5000us */
#define PWM_DUTY_RATIO (uint_16)(5000 / MACHINE_CYCLE)

#define PWM_INACTIVE_TIME (uint_16)(PWM_CYCLE - PWM_DUTY_RATIO)

#define UINT_16_MAX 65535

#define STRAIGHT_ANGLE		180.00
#define RIGHT_ANGLE			90.00
#define ZERO_ANGLE			0.00
#define MAX_ANGLE			STRAIGHT_ANGLE
#define MIN_ANGLE			ZERO_ANGLE
#define MAX_SAFELY_ANGLE	175.00
#define MIN_SAFELY_ANGLE	5.00

#define MIN_DUTY_RATIO 500
#define MAX_DUTY_RATIO 2500

sbit SG90_OUTPUT = P4^6;

void timer2_init(void);
void set_sg90_angle(fp_32 angle);

#endif